package dell.yangzhou01;

import dell.yangzhou.Employee;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamAPITest2 {
    public static void main(String[] args) {
        //Stream终止操作
        //1.匹配与查找
        List<Employee>employees=EmployeeData.getEmployees();
        boolean allMatch = employees.stream().allMatch(e -> e.getSalary() < 60000);
        System.out.println(allMatch);
        boolean noneMatch = employees.stream().noneMatch(employee -> employee.getName().startsWith("雷"));
       //是否不包含？
        System.out.println(noneMatch);
        //返回流中元素，收入大于60000的有多少个
        long count = employees.stream().filter(e -> e.getSalary() > 60000).count();
        System.out.println(count);
        //找出收入中的最大值
        Optional<Integer> max = employees.stream().map(e -> e.getSalary()).max(Integer::compareTo);
        System.out.println(max);
        //输出：Optional[990100]
        //找到最低工资的那个员工
        Optional<Employee> min = employees.stream().min((e1, e2) -> Integer.compare(e1.getSalary(), e2.getSalary()));
        System.out.println(min);
        //归约
        //练习：计算1-10的自然数之和
        List<Integer>list= Arrays.asList(1,2,3,4,5,6,7,8,9,10);
        Integer reduce = list.stream().reduce(0, Integer::sum);
        System.out.println(reduce);
        //练习：计算公司所有员工工资的总和
        List<Employee>employeeList=EmployeeData.getEmployees();
        Stream<Integer> salaryStream = employeeList.stream().map(Employee::getSalary);
        //Optional<Integer> sumSalary = salaryStream.reduce(Integer::sum);或者：
        Optional<Integer> sumSalary = salaryStream.reduce((sal1, sal2) -> sal1 + sal2);
        System.out.println(sumSalary);
        //练习：查找工资大于70000的员工，结果返回一个List或Set
        List<Employee>employees1=EmployeeData.getEmployees();
        List<Employee> employeeList1 = employees1.stream().filter(e -> e.getSalary() > 70000).collect(Collectors.toList());
       employeeList1.forEach(System.out::println);
        System.out.println(",,,,,,,,,,,,,,,,,,,,,,,");
       Set<Employee>set=employees.stream().filter(employee -> employee.getSalary()>70000).collect(Collectors.toSet());
        set.forEach(System.out::println);
        System.out.println("===================");
        //构造器引用
        //匿名类
        Supplier<Employee> sup=new Supplier<Employee>() {
            @Override
            public Employee get() {
                return new Employee();
            }
        };
        //构造器引用
        Supplier<Employee>supF=()->new Employee();
        System.out.println( supF.get());
        System.out.println(",,,,,,,,,,,,,,,");
        Supplier<Employee> supF2=Employee::new;
        System.out.println(supF2.get());
        //数组的引用
        Function<Integer,String[]>func1=length->new String[length];
        String[] arr1 = func1.apply(5);
        System.out.println(Arrays.toString(arr1));
        System.out.println(",,,,,,,,,,,,,,,");
        Function<Integer,String[]>func2=String[]::new;
        String[] arr2 = func2.apply(6);
        System.out.println(Arrays.toString(arr2));
    }
}
